//声明一个域
var app = angular.module("AddApp", []);
//向后台传输数据格式设置
app.config(function($httpProvider) {
	$httpProvider.defaults.headers.put['Content-Type'] = 'application/x-www-form-urlencoded';
	$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';
//			$httpProvider.defaults.headers.common['X-Requested-With'] = 'XMLHttpRequest';
	$httpProvider.transformRequest=function(obj) {    
        var str = [];    
        for (var p in obj) {    
            str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));    
        }    
        return str.join("&");    
    }  
});

app.controller('AddCtrl', function($scope, $http, $window, $timeout) {
//	 声明一个数组来接收复选框的值
//	$scope.selected = [];
	var avoidDup = true;
	// 点击确定方法
	$scope.saveOrUpdate = function() {
		if(avoidDup==false){
			return false;
		}
//		var keywords_type = $scope.selected.join(",");
//		var keyword_t = keywords_type.toString().substr(0,1)==',' ? keywords_type.toString().substr(1) : keywords_type.toString();
		$scope.keywords_type = $("#match_title").attr('class').search('checked') >= 0 ? '百度' : null;
		var vm = {
			"keywords_seek" : $scope.keyword,
			"keywords_type" : $scope.keywords_type
		};
		if(vm.keywords_seek!=undefined){
			avoidDup =false;
			$.post(basePath + 'Keyword/addKeyword.do', vm).success(function(data) {
				if (data.returnCode == 10000) {
					layer.msg(data.returnMessage, 1, 10);
					setTimeout(function(){
						var index = parent.layer.getFrameIndex(window.name);
						try {
							window.parent.$('#Msearch').trigger('click');
						} catch(e) {}
						parent.layer.close(index);
					},1000);
				} else if (data.returnCode == 999994) {
					setUserTimeout(data);
				} else {
					layer.msg(data.returnMessage, 2, -1);
					avoidDup =true;
					return false;
				}
			});
		}
	}
    $scope.isChecked = function(e, isCheck) {
        var _ = $(e);
        if(isCheck == 0 || isCheck == 1) {
            if(isCheck == 0) {
                _.removeClass('checked');
            } else {
                _.addClass('checked');
            }
        } else {
            if(_.attr('class').search('checked') > -1) {
                _.removeClass('checked');
            } else {
                _.addClass('checked');
            }
        }
    }
    $scope.isChecked($("#match_title"), 0);
	//取消关闭弹窗
	$scope.close = function(e){
		window.parent.layer.closeAll();
	}
});
